package forecasting.util;

import forecasting.Model;
import forecasting.converter.parser.DataScrubParser;
import forecasting.converter.parser.RandomForestFileJoiner;
import forecasting.converter.parser.WekaDatasetParser;
import forecasting.validation.CrossValidationDatasetCreator;
import forecasting.validation.ModelEvaluator;

public class Driver {
	
	private static final boolean SAVE_CATEGORIES_FOR_DISCRETE_VALUES = true;
	
	private static final String SCRUB = "-scrub";	
	private static final String EVAL = "-eval";	
	private static final String CROSS_VALIDATE = "-validate";
	private static final String WEKA = "-weka";
	private static final String JOINER = "-joiner";
	

	public static void main(String[] args) {
		if (args.length > 1) {
			if (args[0].equals(EVAL)) {
				evaluator(args);
			} else if (args[0].equals(SCRUB)) {
				dataScrub(args);
			} else if (args[0].equals(CROSS_VALIDATE)) {
				crossValidate(args);
			} else if (args[0].equals(WEKA)) {
				wekaParser(args);
			} else if (args[0].equals(JOINER)) {
				joiner(args);
			}			
		} else {
			printUsage();			
		}					
	}
	
	private static void evaluator(final String[] args) {
		if (args.length < 3) {
			System.out.println("Intrain and outTrain files not specified");
			printUsage(EVAL + " inTrain.txt outTrain.txt");
			System.exit(1);
		}
		
		final String inTestFile = args[1];
		final String outTestFile = args[2];
				
		ModelEvaluator eval = new ModelEvaluator(new Model() {
			public double run(final String line) {
				return 0;
			}
		});
		
		eval.evaulate(inTestFile, outTestFile);		
		eval.reportErrors();		
	}
	
	private static void dataScrub(final String[] args) {
		if (args.length != 3) {
			System.out.println("Intrain and outTrain files not specified");
			printUsage(SCRUB + " inTrain.txt output.txt");
			System.exit(-1);
		}
	
		final String inputFileName = args[1];
		final String outputFileName = args[2];
									
		final DataScrubParser parser = new DataScrubParser(SAVE_CATEGORIES_FOR_DISCRETE_VALUES);
		parser.run(inputFileName, outputFileName);
		
		System.out.println("outputFileName = " + outputFileName);		
		System.out.println("Done!");
	}
	
	private static void crossValidate(final String[] args) {
		if (args.length < 3) {
			System.out.print("You must specify a input and output dataset!");
			printUsage(CROSS_VALIDATE + 
					"intrain.txt outtrain.txt *percent *output_identifier" +
					" Percent is in decimal format, for 60%, use 0.6");
			System.exit(-1);
		}
		final String inDataset = args[1];
		final String outDataset = args[2];
		
		float trainingPercentage = 0.6f;
		
		if (args.length == 4) {
			trainingPercentage = Float.parseFloat(args[3]);
		}
		
		String fileIdentifier = "set";
		
		if (args.length == 5) {
			fileIdentifier = args[4];
		}
					
		CrossValidationDatasetCreator.createCrossValidationDataSet(inDataset, outDataset, trainingPercentage, fileIdentifier);		
		System.out.println("Cross Validation files created");
	}
	
	public static void wekaParser(final String[] args) {
	// validate input parameters
			if(args.length != 3) {
				System.out.println(args.length);
				System.out.println("Converter <file name> <output file name minus extensions>");
				printUsage(WEKA + 
						"intrain.txt output.txt");
				System.exit(-1);
			}
			
			final String inputFileName = args[1];
			final String outputFileName = args[2];
				
			final WekaDatasetParser parser = new WekaDatasetParser(SAVE_CATEGORIES_FOR_DISCRETE_VALUES);
			parser.run(inputFileName, outputFileName);
			System.out.println("finished!");
	}
	
	public static void joiner(final String[] args) {
		// validate input parameters
		if(args.length != 4) {
			System.out.println(args.length);
			System.out.println("RandomForestFileJoiner <dataset_file> <expected_prediction_file> <output_file>");
			System.exit(-1);
		}
		
		final String datasetFile = args[1];
		final String predictionFile = args[2];
		final String outputFile = args[3];
		
		final RandomForestFileJoiner joiner = new RandomForestFileJoiner();
		joiner.run(datasetFile, predictionFile, outputFile);
		System.out.println("Done!!!");
	}
	
	
	
	private static void printUsage() {
		System.out.println(
		String.format("usage: (%s|%s|%s)", EVAL, SCRUB, CROSS_VALIDATE));
	}
	
	private static void printUsage(final String extra) {
		System.out.println("usage: " + extra);
	}
}
